﻿/*
 * Twipler - Twitter Web Client with features.
 *  
 *  Copyright (C) 2009, 2010. Ian Quigley
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details. 
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */

using System;
using Twipler.App;
using Twipler.Data.Access;
using Twipler.Data.Entity.Theme;
using Twipler.Data.Entity.Twipler;
using Twipler.WebApp;

using NHibernate.Criterion;
using System.Web.UI.WebControls;
using Style=Twipler.Data.Entity.Theme.Style;

namespace Settings.Theme
{
    [RequireUser(VisitorRole.StyleCreator)]
    public partial class StyleList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                UpdateDisplay();
        }

        private void UpdateDisplay()
        {
            if (Current.Visitor.HasRole(VisitorRole.Admin))
            {
                grdDesigns.DataSource = Factory.Style.GetAll();
            }
            else
            {
                grdDesigns.DataSource = Factory.Style.GetByCriteria(
                    Expression.Or(
                            Expression.Eq("State", (int)StyleState.Published),
                        Expression.And(
                            Expression.Eq("CreatedBy", Current.Visitor),
                            Expression.Eq("State", (int)StyleState.Private)))
                    );
            }
            grdDesigns.DataBind();
        }

        protected void btnNew_Click(object source, EventArgs e)
        {
        }

        protected void editDesign_Update()
        {
            UpdateDisplay();
        }

        protected void grdDesign_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                Button btnDelete = (Button)e.Item.FindControl("btnDelete");

                if (((Twipler.Data.Entity.Theme.Style)e.Item.DataItem).UseCount > 0)
                {
                    btnDelete.CssClass = "greybutton";
                    btnDelete.Enabled = false;
                }
                else
                {
                    btnDelete.Visible = ((Twipler.Data.Entity.Theme.Style)e.Item.DataItem).UserCanModify();
                    btnDelete.Attributes.Add("onclick", "return window.confirm('Confirm deletion of style');");
                }
            }
        }

        protected void grdDesigns_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            Style style = GetStyle(e);
            if (style == null)
                return;

            switch (e.CommandName)
            {
                case "Source":
                    Current.EntityEdit = style;
                    lblTitle.Text = string.Format("Editting: {0}", style.Name);
                    txtBox.Text = style.Content;
                    litErrors.Text = "";
                    mpeTextEdit.Show();
                    break;

                case "Delete":
                    Factory.Style.Delete(style);
                    UpdateDisplay();
                    break;

                case "New":
                    editDesign.Entity = style;
                    editDesign.Visible = true;
                    editDesign.UpdateDisplay();
                    mpeEditEntity.Show();
                    break;

                case "Edit":
                    editDesign.Entity = style;
                    editDesign.UpdateDisplay();
                    mpeEditEntity.Show();
                    break;
            }
        }

        private Style GetStyle(DataGridCommandEventArgs e)
        {
            int styleId;
            
            if (!int.TryParse(e.Item.Cells[0].Text, out styleId))
                styleId = 0;

            return e.CommandName == "New" ? new Style() : Factory.Style.GetById(styleId, false);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Style style = (Style)Current.EntityEdit;
            style.Content = txtBox.Text;
            
            Factory.Style.SaveOrUpdate(style);
            
            if (Current.Visitor.Style.Id == style.Id)
            {
                Current.Visitor.Style = style;
                Current.StylesheetProducer = new Twipler.App.Render.StylesheetProducer();
            }
        }
    }
}